<?php
/*
	
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['gameId'])){
		$params['gameId']=trim($_RQ['gameId']);
		$return['params']['gameId']=$params['gameId'];
	}
	$fields=array('createtime','cmCode','gameId','level','awardName','money','playerInfo','status','note');
	$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>$fields,'orderby'=>'id desc');
	$list=$this->get_list('redpacker',$parameter,$total);
	if($list){
		foreach($list as &$item){
			
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array (
		  'type' => 'list',
		  'tabs' => 
		  array (
		    0 => 
		    array (
		      'op' => 'list',
		      'title' => '红包列表',
		    ),
		  ),
		  'list' => 
		  array (
		    'data' => 'list',
		    'list' => 
		    array (
		      0 => 
		      array (
		        'type' => 'string',
		        'name' => 'gameId',
		        'title' => '活动编号',
		      ),
		      1 => 
		      array (
		        'name' => 'level',
		        'type' => 'string',
		        'title' => '奖项等级',
		      ),
		      
		      3 => 
		      array (
		        'name' => 'awardName',
		        'type' => 'string',
		        'title' => '奖品名称',
		      ),
		      4 => 
		      array (
		        'name' => 'money',
		        'type' => 'string',
		        'title' => '红包金额',
		      ),
		      5 => 
		      array (
		        'name' => 'playerInfo',
		        'type' => 'string',
		        'title' => '中奖者',
		      ),
		      6 => 
		      array (
		        'name' => 'status',
		        'type' => 'switch',
		        'title' => '状态',
		      ),
			  7 => 
		      array (
		        'name' => 'note',
		        'type' => 'string',
		        'title' => '原因',
		      ),
		    ),
		  ),
		  'delall' => false,
		       'del_one' => false,
		  'edit' => false,
		  'search' =>
		    array(
		  0 =>
		  array (
		        'name' => 'gameId',
				'width'=>'15',
		        'type' => 'string',
		        'title' => '活动编号',
		  ),
		  ),
		);
$return['isrecommand_xuanxiang']=array (
  1 => '首页推荐',
  2 => '新品推荐',
  3 => '热卖推荐',
  4 => '折扣商品',
  5 => '限时卖',
);


	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('redpacker',array('id'=>$id));
			$item['note']=json_decode($item['note'],true);
			$item['shuxing']=';';
		if($item['ishot']){
			$item['shuxing'].='3;';
		}if($item['isrecommand']){
			$item['shuxing'].='1;';
		}if($item['isnew']){
			$item['shuxing'].='2;';
		}if($item['isdiscount']){
			$item['shuxing'].='4;';
		}if($item['istime']){
			$item['shuxing'].='5;';
		}

	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array (
  'type' => 'edit',
  'post' => 'post',
  'edit_tab' => '基本信息',
  'edits' => 
  array (
    0 => 
    array (
      'title' => '基本信息',
      'list' => 
      array (
        0 => 
        array (
          'name' => 'title',
          'type' => 'string',
          'title' => '商品名称',
          'help' => '商品名称',
        ),
        1 => 
        array (
          'name' => 'unit',
          'type' => 'string',
          'title' => '商品单位',
          'help' => '如:x个/件/包',
        ),
        2 => 
        array (
          'name' => 'allowtime',
          'type' => 'time_range',
          'title' => '限时卖时间',
        ),
        3 => 
        array (
          'name' => 'type',
          'type' => 'select',
          'title' => '商品类型',
          'options' => 'type_xuanxiang',
		  'option_title'=>'name'
        ),
		 20 => 
        array (
          'name' => 'shuxing',
          'type' => 'checkbox',
          'title' => '商品属性',
          'options' => 'isrecommand_xuanxiang',
        ),
        4 => 
        array (
          'name' => 'thumb',
          'type' => 'image',
          'title' => '商品图',
        ),
        5 => 
        array (
          'name' => 'description',
          'type' => 'images',
          'title' => '其他图片',
        ),
        6 => 
        array (
          'name' => 'goodssn',
          'type' => 'string',
          'title' => '商品编号',
        ),
        7 => 
        array (
          'name' => 'productsn',
          'type' => 'string',
          'title' => '商品条码',
        ),
        8 => 
        array (
          'name' => 'marketprice',
          'type' => 'string',
          'title' => '销售价',
        ),
        9 => 
        array (
          'name' => 'productprice',
          'type' => 'string',
          'title' => '市场价',
        ),
        10 => 
        array (
          'name' => 'costprice',
          'type' => 'string',
          'title' => '成本价',
        ),
        11 => 
        array (
          'name' => 'originalprice',
          'type' => 'string',
          'title' => '原价',
        ),
        12 => 
        array (
          'name' => 'weight',
          'type' => 'string',
          'title' => '重量',
        ),
        13 => 
        array (
          'name' => 'total',
          'type' => 'string',
          'title' => '库存',
          'help' => '当前商品的库存数量',
        ),
        14 => 
        array (
          'name' => 'totalcnf',
          'type' => 'radio',
          'title' => '减库存方式',
          'options' => 'totalcnf_xuanxiang',
        ),
        15 => 
        array (
          'name' => 'maxbuy',
          'type' => 'string',
          'title' => '单次最多购买量',
        ),
        16 => 
        array (
          'name' => 'usermaxbuy',
          'type' => 'string',
          'title' => '用户最多购买量',
        ),
        17 => 
        array (
          'name' => 'sales',
          'type' => 'string',
          'title' => '销量',
        ),
        18 => 
        array (
          'name' => 'credit',
          'type' => 'string',
          'title' => '购买积分',
          'help' => '会员购买积分,x如果不填写，则默认为不奖励积分',
        ),
        19 => 
        array (
          'name' => 'status',
          'type' => 'switch',
          'title' => '是否上架',
        ),
      ),
    ),
    1 => 
    array (
      'title' => '商品描述',
      'list' => 
      array (
        0 => 
        array (
          'name' => 'content',
          'type' => 'richtext',
          'title' => '商品描述',
        ),
      ),
    ),
    2 => 
    array (
      'title' => '商品规格',
      'list' => 
      array (
        0 => 
        array (
          'name' => 'spec',
          'type' => 'choose',
          'title' => '商品规格',
          'plugin' => '',
          'action' => 'specs',
        ),
      ),
    ),
  ),
);
$params=array('uniacid'=>$_SESSION['uniacid']);
$fields=array("id","name");
$parameter=array('params'=>$params,'fields'=>$fields);
$return['type_xuanxiang']=$this->get_list("category",$parameter);
$return['totalcnf_xuanxiang']=array (
  1 => '拍下减库存',
  2 => '付款减库存',
  3 => '永不减库存',
);
$return['isrecommand_xuanxiang']=array (
  1 => '首页推荐',
  2 => '新品推荐',
  3 => '热卖推荐',
  4 => '折扣商品',
  5 => '限时卖',
);

	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array_fields(array('title','unit','allowtime','type','thumb','description','goodssn','productsn','marketprice','productprice','costprice','originalprice','weight','total','totalcnf','maxbuy','usermaxbuy','sales','credit','content','spec','status',),$post);
	if(strexists($post['shuxing'],'1')){
		$data['isrecommand']=1;
	}
	if(strexists($post['shuxing'],'2')){
		$data['isnew']=1;
	}
	if(strexists($post['shuxing'],'3')){
		$data['ishot']=1;
	}
	if(strexists($post['shuxing'],'4')){
		$data['isdiscount']=1;
	}
	if(strexists($post['shuxing'],'5')){
		$data['istime']=1;
	}
$data['description']=ijson_encode($data['description']);$data['marketprice']=price($data['marketprice']);$data['productprice']=price($data['productprice']);$data['costprice']=price($data['costprice']);$data['originalprice']=price($data['originalprice']);	$data['spec']=ijson_encode($data['spec']);
	$id=intval($post['id']);
	if($id){
		$this->update('goods',$data,array('id'=>$id));
		exi('修改商品成功！');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$this->insert('goods',$data);
		exi('新增商品成功！');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('redpacker',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('redpacker',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}